package com.ponhu.ea2.goodsCenter.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ponhu.ea2.entity.PhAiPictureMaterialMd;
import com.ponhu.ea2.goodsCenter.provider.mapper.PhAiPictureMaterialMdMapper;
import com.ponhu.ea2.goodsCenter.provider.service.PhAiPictureMaterialMdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ponhu.ea2.support.exception.BusinessRuntimeException;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ponhu.ea2.enums.ResponseCodeEnum.INVENTORY_EDIT_EXCEPTION_800216;
import static com.ponhu.ea2.enums.ResponseCodeEnum.INVENTORY_EDIT_EXCEPTION_800217;

@Service
public class PhAiPictureMaterialMdServiceImpl
        extends ServiceImpl<PhAiPictureMaterialMdMapper, PhAiPictureMaterialMd>
        implements PhAiPictureMaterialMdService {

    @Override
    public boolean updateAiPictureByItemId(String itemId, Long materialId, String materialCode) {

        LambdaQueryWrapper<PhAiPictureMaterialMd> queryWrapper = Wrappers
                .lambdaQuery(PhAiPictureMaterialMd.class)
                .eq(PhAiPictureMaterialMd::getItemId, itemId)
                .last("LIMIT 1");
        PhAiPictureMaterialMd aiPictureMaterialMd = getOne(queryWrapper);

        if (aiPictureMaterialMd.getMaterialId() != null) aiPictureMaterialMd.setId(null);

        aiPictureMaterialMd.setMaterialId(materialId).setMaterialCode(materialCode);

        if (!saveOrUpdate(aiPictureMaterialMd))
            throw new BusinessRuntimeException(INVENTORY_EDIT_EXCEPTION_800217);

        return true;
    }

    @Override
    public List<PhAiPictureMaterialMd> getAiPicturesByItemId(String itemId) {

        LambdaQueryWrapper<PhAiPictureMaterialMd> queryWrapper = Wrappers
                .lambdaQuery(PhAiPictureMaterialMd.class)
                .eq(PhAiPictureMaterialMd::getItemId, itemId);

        return list(queryWrapper);
    }

    @Override
    public PhAiPictureMaterialMd aiPictureQuery(String itemId, Long materialId, String materialCode) {

        LambdaQueryWrapper<PhAiPictureMaterialMd> queryWrapper = Wrappers
                .lambdaQuery(PhAiPictureMaterialMd.class).last("LIMIT 1");
        if (StringUtils.isNotEmpty(itemId))
            queryWrapper.eq(PhAiPictureMaterialMd::getItemId, itemId);
        if (ObjectUtils.isNotEmpty(materialId))
            queryWrapper.eq(PhAiPictureMaterialMd::getMaterialId, materialId);
        if (StringUtils.isNotEmpty(materialCode))
            queryWrapper.eq(PhAiPictureMaterialMd::getMaterialCode, materialCode);

        return getOne(queryWrapper);
    }

    @Override
    public boolean saveOrUpdateBatchByItemIds(List<PhAiPictureMaterialMd> aiPictureList) {
        if (CollectionUtils.isEmpty(aiPictureList)) return false;

        List<String> itemIds = aiPictureList.stream().map(PhAiPictureMaterialMd::getItemId).collect(Collectors.toList());
        Map<String, Integer> map = getListByItemIds(itemIds).stream()
                .filter(ai -> StringUtils.isEmpty(ai.getMaterialCode()))
                .collect(Collectors.toMap(PhAiPictureMaterialMd::getItemId,
                        PhAiPictureMaterialMd::getId, (key1, key2) -> key1));

        aiPictureList.stream().forEach(aiPicture -> aiPicture.setId(map.get(aiPicture.getItemId())));

        return saveOrUpdateBatch(aiPictureList);
    }

    @Override
    public List<PhAiPictureMaterialMd> getListByItemIds(List<String> itemIds) {
        if (CollectionUtils.isEmpty(itemIds)) return Lists.newArrayList();

        LambdaQueryWrapper<PhAiPictureMaterialMd> queryWrapper = Wrappers
                .lambdaQuery(PhAiPictureMaterialMd.class)
                .in(PhAiPictureMaterialMd::getItemId, itemIds);

        return list(queryWrapper);
    }

}
